package com.ch.mafia.service;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.ch.mafia.base.BaseServlet;
import com.ch.mafia.bean.SVoteBean;
import com.ch.mafia.process.VoteProcess;

/**
 * Servlet implementation class SendVoteService
 */
public class SendVoteService extends BaseServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public SendVoteService() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		super.doGet(request, response);
		JSONObject jsonParams = new JSONObject();
		JSONObject jsonResult = new JSONObject();
		try{
			if(request.getParameter("json")!=null){
				jsonParams=new JSONObject(request.getParameter("json"));
				System.out.println("jsonParams:"+jsonParams.toString());
			}
			SVoteBean bean =new SVoteBean();
			bean.setJsonObject(jsonParams);
			VoteProcess processVoteProcess =new VoteProcess();
			processVoteProcess.addVote(bean);
			jsonResult.put("result",SUCCESS);
		}catch (Exception e) {
			e.printStackTrace();
			try{
				jsonResult.put("result",FAIL);
			}catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		response.getWriter().print(jsonResult.toString());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
